import 'package:erp/res/resources.dart';
import 'package:erp/widgets/my_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// AppBar
/// 目前安卓上有两种尺寸，分别为56dp和48dp，目前较常用的高度为48dp
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  MyAppBar(
      {Key? key,
      this.bottom,
      this.titleColor,
      this.backgroundColor,
      this.title = '',
      this.centerTitle = '',
      this.actionName = '',
      this.backImg = 'assets/images/back_black.png',
      this.backImgColor,
      this.onPressed,
      this.isBack = true})
      : super(key: key);

  final PreferredSizeWidget? bottom;
  final Color? titleColor;
  final Color? backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final Color? backImgColor;
  final String actionName;
  final VoidCallback? onPressed;
  final bool isBack;

  @override
  Widget build(BuildContext context) {
    Color _backgroundColor = backgroundColor ?? Colours.white;
    TextStyle _titleTextStyle = TextStyle(fontSize: 18.0, color: titleColor ?? Colours.white);

    final SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(_backgroundColor) == Brightness.dark
        ? SystemUiOverlayStyle.light
        : SystemUiOverlayStyle.dark;

    final Widget back = isBack
        ? IconButton(
            onPressed: () {
              FocusManager.instance.primaryFocus?.unfocus();
              Navigator.maybePop(context);
            },
            tooltip: 'Back',
            padding: const EdgeInsets.all(12.0),
            icon: Image.asset(backImg, color: backImgColor),
          )
        : SizedBox.shrink();

    final Widget action = actionName.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: Theme(
              data: Theme.of(context).copyWith(
                buttonTheme: const ButtonThemeData(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  minWidth: 60.0,
                ),
              ),
              child: MyButton(
                key: const Key('actionName'),
                fontSize: 14,
                minWidth: null,
                text: actionName,
                textColor: Colours.text,
                backgroundColor: Colors.transparent,
                onPressed: onPressed,
              ),
            ),
          )
        : SizedBox.shrink();

    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        child: Text(
          title.isEmpty ? centerTitle : title,
          style: _titleTextStyle,
        ),
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
      ),
    );

    Widget main = Stack(
      alignment: Alignment.centerLeft,
      children: <Widget>[
        titleWidget,
        back,
        action,
      ],
    );

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: _backgroundColor,
        child: SafeArea(
          child: bottom != null
              ? Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Flexible(
                      child: ConstrainedBox(
                        constraints: BoxConstraints(maxHeight: 48),
                        child: main,
                      ),
                    ),
                    bottom!,
                  ],
                )
              : main,
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(48.0 + (this.bottom?.preferredSize.height ?? 0.0));
}
